﻿using AuthCenter.CSM.Common;
using AuthCenter.CSM.Dto;
using ExcelConfig;
using Newtonsoft.Json;
using SanlieHash;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthCenter.CSM.plugins
{
    internal class InfoPlug : AuthCenterPluginBase
    {
        protected override string ListenUri { get; } = HttpCommon.SiteName + "/info";
        protected override async Task OnRequest(AuthCenterSession session)
        {
            if (session.IsPost(HttpCommon.SiteName+"/info"))
            {
                var output = new AuthCenterOutput();
                output.Success = true;

                if (session.IsAuthenticated)
                {
                    var data = await session.GetData<AuthCenterInput>();
                    switch (data.Method)
                    {
                        case "info": // 用户信息
                            var info = session.db_auth.GetUserInfo(session.UserID);
                            if (info != null)
                            {
                                var pass = AppConfig.Text("敏感字段密钥");

                                output.Params["name"] = info.Name;
                                output.Params["gender"] = info.Gender;
                                output.Params["email"] = FastAES.Decrypt(info.Email, pass);
                                output.Params["phone"] = FastAES.Decrypt(info.Phone, pass);
                                output.Params["tel"] = FastAES.Decrypt(info.Tel, pass);
                                output.Params["address"] = info.Address;
                                output.Params["P1"] = info.P1;
                                output.Params["P2"] = info.P2;
                                output.Params["P3"] = info.P3;
                                output.Params["P4"] = info.P4;
                                output.Params["P5"] = info.P5;
                                output.Params["P6"] = info.P6;
                            }
                            else
                            {
                                output.Success = false;
                            }
                            break;
                        case "permission":
                            var permission = session.db_auth.GetPermission(session.UserID, session.AppID);
                            if (permission != null)
                            {
                                output.Params["permission"] = permission.PermissionContent;
                            }
                            else
                            {
                                output.Success = false;
                            }
                            break;
                        default:
                            output.Success = false;
                            break;
                    }
                }
                else
                {
                    output.Success = false;
                }

                await session.Send(200, output)
                        .AnswerAsync();//直接回应
            }
            else if (session.IsGet("/info"))
            {
                var output = new AuthCenterOutput();
                output.Success = session.IsAuthenticated;

                if (session.IsAuthenticated)
                {
                    output.Params["uid"] = session.UserID.ToString();
                }

                await session.Send(200, output)
                        .AnswerAsync();//直接回应
                return;
            }

            //无法处理，调用下一个插件
            await session.InvokeNext();
        }
    }
}
